'use strict';

import AddressComponent from '../../prototype/addressComponent'
import pinyin from "pinyin"
import Cities from '../../models/v1/cities'

class CityHandle extends AddressComponent {
  constructor() {
    super()
    // 绑定this
    this.getCity = this.getCity.bind(this);
    this.pois = this.pois.bind(this)
  }
  /**
   * 定位获取城市信息
   * @url /api/v1/cities?type=guess
   * @param {string} type 选择获取模式
   * @param {*} next 
   */
  async getCity(ctx, next) {
    let type = ctx.query.type;
    let cityInfo;
    try {
      switch (type) {
        case 'guess':
          const city = await this.getCityName(ctx.request);
          cityInfo = await Cities.cityGuess(city);
          break;
        case 'hot':
          cityInfo = await Cities.cityHot();
          break;
        case 'group':
          cityInfo = await Cities.cityGroup()
          break;
        default:
          ctx.body = {
            name: 'ERROR_QUERY_TYPE',
            message: '参数错误',
          }
          return
      }
      ctx.body = cityInfo
    } catch (err) {
      ctx.body = {
        name: 'ERROR_DATA',
        message: '获取数据失败',
      }
    }
  }
  /**
   * 通过ip获取地址
   * @param {object} req 
   */
  async getCityName(req) {
    try {
      const cityInfo = await this.guessPosition(req)
      console.log(cityInfo)
      /*
      汉字转换成拼音
       */
      const pinyinArr = pinyin(cityInfo.city, {
        style: pinyin.STYLE_NORMAL,
      });
      let cityName = '';
      pinyinArr.forEach(item => {
        cityName += item[0];
      })
      return cityName;
    } catch (err) {
      return '东莞'
    }
  }

  /**
   * 通过id搜索城市信息
   * @url /api/v1//cities:id
   * @param {number} id 城市id
   */
  async getCityById (ctx, next) {
    const cityid = ctx.params.id;
    // 判断非数字返回错误信息
    if (isNaN(cityid)) {
      ctx.body = {
        name: 'ERROR_QUERY_TYPE',
        message: '参数错误',
      }
      return
    }
    try {
      const cityInfo = await Cities.getCityById(cityid);
      ctx.body = cityInfo;
    } catch (err) {
      ctx.body = {
        name: 'ERROR_DATA',
        message: '获取数据失败',
      }
    }
  }

  /**
   * 通过经纬度搜索精确位置
   * @url api/v1/pois/:geohash
   * @params {number,number} 经纬度以逗号隔开 例：23.00452,113.77591
   */
  async pois (ctx,next) {
    try {
      const geohash = ctx.params.geohash
      if (geohash.indexOf(',') == -1) {
        ctx.body = {
          status: 0,
          type: 'ERROR_PARAMS',
          message: '参数错误'
        }
        return
      }

      const poisArr = geohash.split(',')
      const result = await this.getpois(poisArr[0],poisArr[1])
      const address = {
        address: result.result.address,
        city: result.result.address_component.province,
        geohash,
        latitude: poisArr[0],
        longitude: poisArr[1],
        name: result.result.formatted_addresses.recommend,
      }

      ctx.body = address
    } catch(err) {
      console.log('getpois返回信息失败', err);
      ctx.body = {
        status: 0,
        type: 'ERROR_DATA',
        message: '获取数据失败'
      }
    }
  }
}
export default new CityHandle()